package cn.itsource.basic.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.itsource.basic.domain.BaseTreeNode;
import cn.itsource.org.pojo.domain.Department;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
public class TreeUtil {

    //java8构建树

    /**
     * 将平铺的List<T>转换成Tree
     * @param listNodes
     * @param <T>
     * @return
     */
    public static <T extends BaseTreeNode> List<T> getTree(List<T> listNodes){
//        判断当前listNodes 是否为空
        if (CollectionUtil.isNotEmpty(listNodes)){
            //根据pid分组 key 都是parentId ,value 所有的permission对象
            Map<Long, List<T>> listMap = listNodes.stream().collect(Collectors.groupingBy(t -> Optional.ofNullable(t.getParentId()).orElse(0L)));
            //得到一级分类,第一级pid==0

            List<T> oneLevelClassify = listMap.get(0L);
            //递归
            List<T> list = getChildList(oneLevelClassify, listMap);
            return list;
        }
        return new ArrayList<>();
    }

/*
    public static <T extends BaseTreeNode> List<T> getTree1(List<T> listNodes){

        HashMap<Long, T> map = new HashMap<>();
        List<Long> ids = new ArrayList<>();

        for (T listNode : listNodes) {
            Long id = listNode.getId();
            map.put(id,listNode);
            ids.add(id);
        }


        HashMap<Long, String> result = new HashMap<>();
        String initStr = "";
        for (Long id : ids) {
            getNode(id, map);

        }
        return null;
    }*/

    public static <T extends BaseTreeNode> void getNode(Long id, HashMap<Long,T> map,List<Long> parentIds) {

        T t = map.get(id);
        Long parentId = t.getParentId();

        if (parentId != null){
            parentIds.add(parentId);
            getNode(parentId,map,parentIds);
        }
    }

    public static void main(String[] args) {
        Department department1 = new Department();
        department1.setId(1L);
        department1.setParentId(null);

        Department department2 = new Department();
        department2.setId(3L);
        department2.setParentId(null);

        Department department3 = new Department();
        department3.setId(7L);
        department3.setParentId(3L);

        Department department4 = new Department();
        department4.setId(8L);
        department4.setParentId(3L);

        Department department5 = new Department();
        department5.setId(9L);
        department5.setParentId(8L);

        Department department6 = new Department();
        department6.setId(10L);
        department6.setParentId(8L);
        HashMap<Long,Department> map = new HashMap<>();
        ArrayList<Department> departments = new ArrayList<>();

        map.put(department1.getId(),department1);
        map.put(department2.getId(),department2);
        map.put(department3.getId(),department3);
        map.put(department4.getId(),department4);
        map.put(department5.getId(),department5);
        map.put(department6.getId(),department6);

        departments.add(department1);
        departments.add(department2);
        departments.add(department3);
        departments.add(department4);
        departments.add(department5);
        departments.add(department6);



        for (Department department : departments) {

            ArrayList<Long> parentIds = new ArrayList<>();
            getNode(department.getId(), map, parentIds);
            Collections.reverse(parentIds);
            parentIds.add(department.getId());

            String dirPath = "/" + StringUtils.join(parentIds, "/");
            System.out.println(department.getId()+"   "+dirPath);
            parentIds.clear();
        }


    }



    /**
     * 获取子节点
     * @param classifies
     * @param groupMap
     * @param <T>
     * @return
     */
    private static <T extends BaseTreeNode> List<T> getChildList(List<T> classifies, Map<Long, List<T>> groupMap) {
        //判断一级的数据是否为空
       if (CollectionUtil.isNotEmpty(classifies)){
           //遍历 tree 父级对象
           for (T tree:classifies){
               //所有的二级的list对象
               List<T> classifyList = groupMap.get(tree.getId());
               //判断是否为空
               if (!CollectionUtils.isEmpty(classifyList)) {
                   tree.setChildren((List<BaseTreeNode>)classifyList);
                   getChildList(classifyList, groupMap);
               } else {
                   tree.setChildren(null);
               }
           }
           return classifies;
       }
       return new ArrayList<>();
    }


}
